package com.jt.service;

import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public String login(User user) {
        String password= user.getPassword();
        String md5pass= DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(md5pass);
        User userDB=userMapper.findUP(user);
        if (userDB==null){
            return null;
        }
        String uuid= UUID.randomUUID().toString().replace("-","");
        return uuid;
    }

    @Override
    public PageResult getuserlist(PageResult pageResult) {
//      long total=userMapper.getTotal();
//      int size=pageResult.getPageSize();
//      int start=(pageResult.getPageNum()-1) * size;
//      List<User> rows=userMapper.finduserByRows( start,size);
//
//        return pageResult.setTotal(total).setRows(rows);
        long total = userMapper.getTotal();
        int size = pageResult.getPageSize();
        int start = (pageResult.getPageNum() - 1) * size;
       String query= pageResult.getQuery();
        List<User> rows = userMapper.finduserByRows(start, size,query);
        return pageResult.setTotal(total).setRows(rows);
    }

    @Override
    public void uptatestatus(User user) {
        user.setUpdated(new Date());
        userMapper.updatestatus(user);
    }

    @Override
    public void insertuser(User user) {
String password= user.getPassword();
String md5pass=DigestUtils.md5DigestAsHex(password.getBytes());
user.setPassword(md5pass).setStatus(true).setCreated(new Date()).setUpdated(user.getCreated());

        userMapper.insertuser(user);

    }

    @Override
    public User xiugaiuser(Integer id) {
        return userMapper.finduserByid(id);
    }

    @Override
    public void upatatate(User user) {
//        参数名称	参数说明	备注
//        ID	用户ID号	不能为null
//        phone	手机信息	不能为null
//        email	邮箱地址	不能为null
        userMapper.updatedate(user);
    }
    @Transactional//进行事物的控制
    @Override
    public void deleteuser(Integer id) {
        userMapper.deleteuser(id);
    }
}
